package com.atguigu.service.impl;

import com.atguigu.common.CustomException;
import com.atguigu.entity.Category;
import com.atguigu.entity.Dish;
import com.atguigu.entity.Setmeal;
import com.atguigu.mapper.CategoryMapper;
import com.atguigu.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private DishServiceImpl dishService;
    @Resource
    private SetmealServiceImpl setMealService;

    //自定义业务异常
    public void myRemove(String ids){
        //添加查询条件，根据分类id进行查询
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<Dish>();
        dishLambdaQueryWrapper = dishLambdaQueryWrapper.eq(Dish::getCategoryId, ids);
        long count = dishService.count(dishLambdaQueryWrapper);
        if(count>0){
            //已经关联菜品，抛出业务异常
            throw new CustomException("该分类中关联了菜品,不能删除");
        }
        ////添加查询条件，根据分类id进行查询
        LambdaQueryWrapper<Setmeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper  = setMealLambdaQueryWrapper.eq(Setmeal::getCategoryId, ids);
        long count1 = setMealService.count(setMealLambdaQueryWrapper);
        if(count1>0){
            //已经关联套餐，抛出业务异常
            throw new CustomException("该分类中关联了套餐,不能删除");
        }

        //没有关联菜品和套餐，直接删除
        super.removeById(ids);

    }
}
